
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
/**
 *
 * @author Andrew Markley && Nick McCleery
 */
public class Spades extends JFrame implements Runnable, ActionListener{

   	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int width = 855;
	private static final int height = 710;
    private Board theGame;
    private State state;
    //stuff for the menus
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;
    Spades runner;
    
	public Spades()
	{
            super("Spades");
            setSize(width,height);
            setLocation(200, 40);
            setResizable(false);
            setAlwaysOnTop(false);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            theGame = new Board(this);
            state = new State();
            ((Component)theGame).setFocusable(true);
            getContentPane().add(theGame);
            createMenuBar();
            setJMenuBar(menuBar);
            run();
	}
	private void createMenuBar() {
		//Create the menu bar.
		menuBar = new JMenuBar();

		//Build the first menu.
		menu = new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_A);
		menu.getAccessibleContext().setAccessibleDescription("file");
		menuBar.add(menu);

		//a group of JMenuItems
		menuItem = new JMenuItem("New Game",KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription("new_game");
		menuItem.setActionCommand("new_game");
		menuItem.addActionListener(this);
		menu.add(menuItem);

		menuItem = new JMenuItem("Edit Settings");
		menuItem.setMnemonic(KeyEvent.VK_B);
		menuItem.getAccessibleContext().setAccessibleDescription("edit_settings");
		menuItem.setActionCommand("settings");
		menuItem.addActionListener(this);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Statistics");
		menuItem.getAccessibleContext().setAccessibleDescription("statistics");
		menuItem.setActionCommand("stats");
		menuItem.addActionListener(this);
		menu.add(menuItem);
		
		menu.addSeparator();
		
		menuItem = new JMenuItem("Exit");
		menuItem.getAccessibleContext().setAccessibleDescription("exit");
		menuItem.setActionCommand("exit");
		menuItem.addActionListener(this);
		menu.add(menuItem);
		
		//new menu category
		
		menu = new JMenu("Help");
		menu.setMnemonic(KeyEvent.VK_N);
		menu.getAccessibleContext().setAccessibleDescription("help");
		menuItem.addActionListener(this);
		menuBar.add(menu);
		
		menuItem = new JMenuItem("About");
		menuItem.getAccessibleContext().setAccessibleDescription("about");
		menuItem.setActionCommand("about");
		menuItem.addActionListener(this);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Help");
		menuItem.getAccessibleContext().setAccessibleDescription("help");
		menuItem.setActionCommand("help");
		menuItem.addActionListener(this);
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Contact");
		menuItem.getAccessibleContext().setAccessibleDescription("contact");
		menuItem.setActionCommand("contact");
		menuItem.addActionListener(this);
		menu.add(menuItem);
	}
	public static void main( String args[] )
	{
		@SuppressWarnings("unused")
		Spades runner = new Spades();
	}
   @SuppressWarnings("static-access")
public void run()
   {
   	try
   	{
   		while(true)
   		{
   		   Thread.currentThread().sleep(8);
           theGame.update(state);
           if(state.getState()==10)
           {
        	   end();
           }
         }
      }catch(Exception e)
      {
      }
  	}
   	public JFrame getJFrame()
   	{
   		return this;
   	}
    public void actionPerformed(ActionEvent e) {
    	if(e.getActionCommand()!=null)
        {
        	if(e.getActionCommand().equals("new_game"))
        	{
        		state.setState(11);
        		theGame.update(state);
        		//System.out.println(e.getActionCommand() +" " +state.getState());
        	}
        	else if(e.getActionCommand().equals("settings"))
        	{
        		state.setState(1);
        		theGame.update(state);
        		//mainMenu.setVisible(true);
        	}
        	else if(e.getActionCommand().equals("stats"))
        	{
        		//state.setState(2);
        		//mainMenu.setVisible(false);
        	}
        	else if(e.getActionCommand().equals("exit"))
        	{
        		end();
        		System.out.println(e.getActionCommand());
        	}
        }
    }
    public void end() 
    {                
    	WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);                
    	Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);        
    }
}
